package cn.jiyun.controller;

import cn.jiyun.pojo.Dept;
import cn.jiyun.pojo.Emp;
import cn.jiyun.pojo.EmpQuery;
import cn.jiyun.service.DeptService;
import cn.jiyun.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class EmpController {

    @Autowired
    EmpService empService;
    @Autowired
    DeptService deptService;

    @GetMapping("findAll")
    @ResponseBody
    public List<Emp> findAll(){
        return empService.findAll();
    }

    /**
     * 查询员工信息
     * @return
     */
    @RequestMapping("findEmp")
    public String findEmp(Model model){
        List<Emp> empList = empService.findAll();
        model.addAttribute("empList",empList);
        System.out.println("yyyyyyyyyy");
        return "emp.html";
    }

    /**
     * 进入页面
     *
     * @return
     */
    @RequestMapping("toPage")
    public String toPage(){
        return "list.html";
    }

    /**
     * 查询所有部门
     *
     * @return
     */
    @RequestMapping("findDept")
    @ResponseBody
    public List<Dept> findDept(){
        return deptService.findDeptAll();
    }

    /**
     * 查询所有员工
     *
     * @param empQuery
     * @return
     */
    @PostMapping("findAllEmp")
    @ResponseBody
    public PageInfo<Emp> findAll(@RequestBody EmpQuery empQuery){
        PageHelper.startPage(empQuery.getPageNum(), 2);
        List<Emp> empList = empService.findAllEmp(empQuery.getEmp());
        PageInfo<Emp> page =new PageInfo<Emp>(empList);
        return page;
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(){
        return "add.html";
    }
    /**
     * 添加员工信息
     *
     * @param emp
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public String add(@RequestBody Emp emp){
        String result = "SUCC";
        try {
            empService.insertEmp(emp);
        } catch (Exception e) {
            e.printStackTrace();
            result = "FAIL";
        }
        return result;
    }

    /**
     * 进入修改页面
     *
     * @param eid
     * @param model
     * @return
     */
    @RequestMapping("toUpdate")
    public String toUpdate(int eid,Model model){
        model.addAttribute("eid",eid);
        return "update.html";
    }

    /**
     * 根据id查询emp对象
     *
     * @param eid
     * @return
     */
    @RequestMapping("findById")
    @ResponseBody
    public Emp findById( int eid){
        return empService.findById(eid);
    }

    /**
     * 修改员工信息
     *
     * @param emp
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public String update(@RequestBody Emp emp){
        String result = "SUCC";
        try {
            empService.updateById(emp);
        } catch (Exception e) {
            e.printStackTrace();
            result = "FAIL";
        }
        return result;
    }

    /**
     * 单条删除
     *
     * @param eid
     * @return
     */
    @RequestMapping("deleteById")
    @ResponseBody
    public String deleteById(int eid){
        String result = "SUCC";
        try {
            empService.deleteById(eid);
        } catch (Exception e) {
            e.printStackTrace();
            result = "FAIL";
        }
        return result;
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("batchDelete")
    @ResponseBody
    public String batchDelete(int[] ids){
        String result = "SUCC";
        try {
            if(ids.length>0){
                for (int i = 0; i < ids.length; i++) {
                    empService.deleteById(ids[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "FAIL";
        }
        return result;
    }





}
